package com.orders.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.orders.pojo.Customer;
import com.orders.pojo.User;


public class Exist {
	private static Connection conn;
	private static PreparedStatement ps;
	private static ResultSet rs;
	public static boolean exist(Object obj){
		conn = ConnectionFactory.getConnection();
		String username = null;
		
		try{
			if(obj instanceof User){
				User user = (User)obj;
				username = user.getUsername();
				ps = conn.prepareStatement("select count(*) from user where username = ?");
			}
			
			if(obj instanceof Customer){
				Customer customer = (Customer)obj;
				username = customer.getUsername();
				ps = conn.prepareStatement("select count(*) from customer where username = ?");
			}
			
			ps.setString(1, username);
			rs = ps.executeQuery();
			rs.next();
			int count = rs.getInt(1);
			
			if(count != 0){
				return false;
			}
			
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return true;
	}
}
